package com.chudian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chudian.entity.PaymentInfo;
import com.chudian.enums.PayType;
import com.chudian.mapper.PaymentInfoMapper;
import com.chudian.service.PaymentInfoService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service
@Slf4j
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    /**
     * 微信记录支付日志
     * @param plainText
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createPaymentInfo(String plainText) {

        log.info("微信记录支付日志");

        //解析明文
        Gson gson = new Gson();
        Map<String,Object> hashMap =
                gson.fromJson(plainText, HashMap.class);
        //订单号
        String orderNo = (String) hashMap.get("out_trade_no");
        //微信支付订单号
        String transactionId = (String) hashMap.get("transaction_id");
        //交易类型
        String tradeType = (String) hashMap.get("trade_type");
        //交易状态
        String tradeState = (String) hashMap.get("trade_state");
        //订单金额
        Map<String,Object> amount = (Map) hashMap.get("amount");
        Integer payer_total = ((Double) amount.get("payer_total")).intValue();


        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setTransactionId(transactionId);
        paymentInfo.setPaymentType(PayType.WXPAY.getType());
        paymentInfo.setTradeType(tradeType);
        paymentInfo.setTradeState(tradeState);
        paymentInfo.setPayerTotal(payer_total);
        paymentInfo.setContent(plainText);

        baseMapper.insert(paymentInfo);

    }

    /**
     * 支付宝记录支付日志
     * @param params
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createPaymentInfoForAlipay(Map<String, String> params) {

        log.info("支付宝记录支付日志");

        //获取商家订单号
        String outTradeNo = params.get("out_trade_no");
        //获取支付宝交易号
        String tradeNo = params.get("trade_no");
        //获取交易状态
        String tradeStatus = params.get("trade_status");
        //获取支付金额
        String totalAmount = params.get("total_amount");
        int total = new BigDecimal(totalAmount).multiply(new BigDecimal(100)).intValue();

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(outTradeNo);
        paymentInfo.setTransactionId(tradeNo);
        paymentInfo.setPaymentType(PayType.ALIPAY.getType());
        paymentInfo.setTradeType("电脑网站支付");
        paymentInfo.setTradeState(tradeStatus);
        paymentInfo.setPayerTotal(total);

        Gson gson = new Gson();
        String paramJson = gson.toJson(params, HashMap.class);
        paymentInfo.setContent(paramJson);

        baseMapper.insert(paymentInfo);
    }

}
